package com.dbq.fast.business.invite.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.invite.adapter.IItemClickListener;
import com.dbq.fast.business.invite.adapter.ShareAdapter;
import com.dbq.fast.business.invite.been.ShareItem;
import com.dbq.fast.util.RudenessScreenHelper;

import java.util.ArrayList;
import java.util.List;


public class ShareWidget {
    private Activity activity; //the target activity use this widget ( not the activity intent to !!!)
    private View maskView; //pass mask view
    private PopupWindow mPopupWindow;
    private View popupView;

    RecyclerView shareListRV;
    ShareAdapter shareAdapter;
    private IItemClickListener iItemClickListener;

    public void setItemClickListener(IItemClickListener itemClickListener){
        this.iItemClickListener = itemClickListener;
    }

    public void init(Activity activity, View maskView){
        this.activity = activity;
        this.maskView = maskView;
        initPopupWindow();
    }

    /**
     * 初始化在线帮助底部小弹窗
     */
    private void initPopupWindow() {
        popupView = activity.getLayoutInflater().inflate(R.layout.share_popupwindow, null);
        adapterPopWindow(popupView);

        mPopupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setAnimationStyle(R.style.anim_menu_bottombar);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), (Bitmap) null));

        mPopupWindow.getContentView().setFocusableInTouchMode(true);
        mPopupWindow.getContentView().setFocusable(true);
        mPopupWindow.setOnDismissListener(() -> {
            maskView.setVisibility(View.GONE);
        });

        initRecycleView();
    }

    private void initRecycleView() {
        shareListRV = popupView.findViewById(R.id.share_list);
        shareListRV.setLayoutManager(new GridLayoutManager(App.getContext(), 4));
//        shareListRV.setHasFixedSize(true);

        shareAdapter = new ShareAdapter();
        shareListRV.setAdapter(shareAdapter);
        shareAdapter.setData(loadData());

        shareAdapter.setItemClickListener(iItemClickListener);
    }

    private List<ShareItem> loadData(){
        List<ShareItem> shareItems = new ArrayList<>();
        Context context = App.getContext();
        shareItems.add(new ShareItem(R.drawable.share_wechat, context.getString(R.string.wechat)));
        shareItems.add(new ShareItem(R.drawable.share_moment, context.getString(R.string.wechat_moment)));
        shareItems.add(new ShareItem(R.drawable.share_qq, context.getString(R.string.qq)));
        shareItems.add(new ShareItem(R.drawable.share_weibo, context.getString(R.string.weibo)));
        shareItems.add(new ShareItem(R.drawable.share_copy, context.getString(R.string.copy_link)));
        return shareItems;
    }

    /**
     * 显示
     */
    public void show(){
        maskView.setVisibility(View.VISIBLE);
        if(mPopupWindow != null){
            mPopupWindow.showAtLocation(popupView, Gravity.BOTTOM, 0, 0);
        }
    }

    public void destory(){
        maskView.setVisibility(View.GONE);
        if(mPopupWindow != null){
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
    }

    private void adapterPopWindow(View popupView) {
        if ("motorola".equals(Build.BRAND) || "Nexus 5".equals(Build.MODEL)) {
            popupView.setPadding(0,
                    (int) RudenessScreenHelper.pt2px(App.getContext(), 2),
                    0,
                    (int) RudenessScreenHelper.pt2px(App.getContext(), 2) + (int) RudenessScreenHelper.dp2px(App.getContext(), 48));
        }
    }

}
